package com.liugl.activitytest

import android.app.Activity

///使用单例管理所有Activity
object ActivityCollector {
    private val activities = ArrayList<Activity>()

    ///添加一个Activity
    fun addActivity(activity: Activity) {
        activities.add(activity)
    }

    ///移除一个Activity
    fun removeActivity(activity: Activity) {
        activities.remove(activity)
    }

    ///结束所有Activity并清空Activity列表
    fun finishAll() {
        ///遍历并停止所有Activity
        for (activity in activities) {
            if (!activity.isFinishing) {
                activity.finish()
            }
        }

        ///清空Activity列表
        activities.clear()
        ///终止当前进程
        android.os.Process.killProcess(android.os.Process.myPid())
    }
}